package ac.neec.mio.ui.fragment;

import java.util.List;

import ac.neec.data.RecordDetail;
import ac.neec.mio.R;
import ac.neec.mio.record.list.DetailCallbackListener;
import ac.neec.mio.ui.graph.HeartRateGraph;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class RecordDetailFragment extends Fragment {

	public static final int SET_NEXT_BUTTON = 1;
	public static final int SET_BACK_BUTTON = 2;

	private TextView dateText;
	private TextView trainingNameText;
	private TextView heartRateTheoryText;
	private TextView heartRatePointText;
	private TextView calorieTheoryText;
	private TextView caloriePointText;
	private TextView measurementTimeText;
	private ImageButton backButton;
	private ImageButton nextButton;
	private RecordDetail detail;
	private int item;
	private DetailCallbackListener listener;
	private RecordListFragment fragment;
	private HeartRateGraph heartRateGraph;
	private FragmentManager fragmentManager;
	private int size;
	private List<Integer> graphData;

	public int getItem() {
		return item;
	}

	public RecordDetailFragment() {
	}

	@Override
	public void onPause() {
		super.onPause();
		RecordListFragment.setShowFlag(false);
	}

	public void setItem(RecordDetail detail, int item, int size) {
		this.detail = detail;
		this.item = item;
		this.size = size;
	}

	public void setCallbackListener(DetailCallbackListener listener) {
		this.listener = listener;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.record_detail, container, false);
		fragmentManager = getFragmentManager();

		initFindViews(view);
		setListener();
		setDisplay(detail);
		setGraphData();
		setHeartRateGraph();
//		buttonVisibility();

		return view;
	}

	private void setGraphData() {
		graphData = detail.getGraph();
	}

	private void setHeartRateGraph() {
		heartRateGraph.createChart(graphData);
	}

	private boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			return false;
		}
		return true;
	}

//	private void buttonVisibility() {
//		if (item == 0) {
//			backButton.setVisibility(View.INVISIBLE);
//		}
//		else if (size - 1 == item) {
//			nextButton.setVisibility(View.INVISIBLE);
//		}
//		else {
//			nextButton.setVisibility(View.VISIBLE);
//			backButton.setVisibility(View.VISIBLE);
//		}
//	}

	private void setDisplay(RecordDetail detail) {
		dateText.setText(detail.getRecordDate().getDate());
		if (detail.getActivities() != null)
			trainingNameText.setText(detail.getActivities().getMajorHeading().getTitleJP() + "\n  "
					+ detail.getActivities().getDetailJP());
		heartRateTheoryText.setText(String.valueOf(detail.getHeartRateTheory()));
		heartRatePointText.setText(String.valueOf(detail.getHeartRatePoint()));
		calorieTheoryText.setText(String.valueOf(detail.getCalorieTheory()));
		caloriePointText.setText(String.valueOf(detail.getCaloriePoint()));
		measurementTimeText.setText(detail.getMeasurementTime());
	}

	private void setListener() {

//		backButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Log.e("", "backButtonClick item = " + item);
//				listener.showBackDetail(item);
//			}
//		});
//		nextButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Log.e("", "nextButtonClick item = " + item);
//				listener.showNextDetail(item);
//			}
//		});
	}

	private void initFindViews(View view) {
		dateText = (TextView) view.findViewById(R.id.text_detail_date);
		trainingNameText = (TextView) view.findViewById(R.id.text_detail_training_variety);
		heartRateTheoryText = (TextView) view.findViewById(R.id.text_detail_heart_rate_theory);
		heartRatePointText = (TextView) view.findViewById(R.id.text_detail_heart_rate_point);
		calorieTheoryText = (TextView) view.findViewById(R.id.text_detail_calorie_theory);
		caloriePointText = (TextView) view.findViewById(R.id.text_detail_calorie_point);
		measurementTimeText = (TextView) view.findViewById(R.id.text_detail_measurement_time);
//		backButton = (ImageButton) view.findViewById(R.id.btn_detail_left);
//		nextButton = (ImageButton) view.findViewById(R.id.btn_detail_right);
		heartRateGraph = (HeartRateGraph) view.findViewById(R.id.graph_heart_rate);
	}

}
